#include "GLTexture.h"
#include "TargaImage.h"

#include <iostream>

using namespace std;

GLTexture::GLTexture(void)
: m_filename(), m_textureID(), m_width(), m_height(), m_magFilterMode(),
m_minFilterMode(), m_textureComponents(), m_textureFormat(), m_dataType(),
m_mipmaps()
{
}

GLTexture::GLTexture(const string filename, const GLint magFilterMode,
                     const GLint minFilterMode, const GLint textureComponents,
                     const GLenum textureFormat, const GLenum dataType, const bool mipmaps)
: m_filename(filename), m_textureID(), m_width(), m_height(), m_magFilterMode(magFilterMode),
m_minFilterMode(minFilterMode), m_textureComponents(textureComponents), m_textureFormat(textureFormat),
m_dataType(dataType), m_mipmaps(mipmaps)
{
}

GLTexture::~GLTexture(void)
{
}

bool GLTexture::initialize(void)
{
    TargaImage image;
    if (!image.load(m_filename))
    {
        std::cerr << "Couldn't load the texture " << m_filename << std::endl;
        return false;
    }
    m_width = image.getWidth();
    m_height = image.getHeight();

    glGenTextures(1, &m_textureID);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, m_textureID);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, m_magFilterMode);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, m_minFilterMode);

    if (m_mipmaps)
    {
        gluBuild2DMipmaps(GL_TEXTURE_2D, m_textureComponents, m_width,
                      m_height, m_textureFormat, m_dataType,
                      image.getImageData());
    }
    else
    {
        // TODO
        std::cerr << "Couldn't load the texture " << m_filename << std::endl;
        return false;
    }

    return true;
}
